package zk;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DruidUtils {
    private static Connection connection;
    private static DataSource dataSource;
    static{
        InputStream resourceAsStream = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        try {
            connection = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }
    public void close(Connection con, PreparedStatement statement, ResultSet resultSet){
        if(con != null && statement != null && resultSet != null)
        {
            try
            {
                resultSet.close();
                statement.close();
                con.close(); }
            catch
            (SQLException e)
            { e.printStackTrace();
            }
        }
    }

}
